const express = require('express')
const multer = require('multer')
const { UPLOAD_PATH } = require('../utils/constant')
const Result = require('../models/Result')
const News = require('../models/News')
const boom = require('boom')
const { decoded } = require('../utils')
const newsService = require('../services/news')

const router = express.Router()
//获取新闻列表

router.get('/NewsList', function(req, res, next) {
    newsService.NewsList(req.query)
      .then(({ list, count, page, pageSize }) => {
        new Result({ list, count, page: +page, pageSize: +pageSize },
          '获取新闻信息成功').success(res)
      }).catch(err => {
      next(boom.badImplementation(err))
    })
  })

//获取新闻分类列表
router.get('/NewsCategoryList', function(req, res, next) {
    newsService.NewsCategoryList().then(category => {
      new Result(category, '获取分类成功').success(res)
    }).catch(err => {
      next(boom.badImplementation(err))
    })
  })




//获取单个
router.get('/getOneNews', function(req, res) {
    const news = req.query.fileName
    //////console.log(news)
    newsService.getOneNews(news).then(News => {
        if (News) {
            new Result(News, ' 单个新闻信息查询成功').success(res)
        } else {
            new Result('单个新闻信息查询失败').fail(res)
        }
    })

})

//添加新闻图片
router.post('/uploadNews1', multer({ dest: `${UPLOAD_PATH}/news` }).single('file'),function(req, res, next) {
        //////console.log(req.file)
        //////console.log(req.body)
        //const News = req.file
        if (!req.file || req.file.length === 0) {
            new Result('上传图片失败').fail(res)
        } else {
            const newsImg = new News(req.file)
            //因为上传的文件没有jpg的图片后缀，所有加一个parse（）来重命名
            newsImg.parse()
                .then(newsImg => {
                    new Result(newsImg, '上传图片成功').success(res)
                })
                .catch(err => {
                    next(boom.badImplementation(err))
                })

        }
    }
)

//添加新闻图片
router.post('/uploadNews', multer({ dest: `${UPLOAD_PATH}/news` }).single('file'),function(req, res, next) {
        ////console.log(req.file)
        ////console.log(req.body)
        //const News = req.file
        if (!req.file || req.file.length === 0) {
            new Result('上传图片失败').fail(res)
        } else {
            const newsImg = new News(req.file)
            //因为上传的文件没有jpg的图片后缀，所有加一个parse（）来重命名
            newsImg.parse(req.body.username)
                .then(newsImg => {
                    new Result(newsImg, '上传图片成功').success(res)
                })
                .catch(err => {
                    next(boom.badImplementation(err))
                })

        }
    }
)

//添加新闻
router.post('/createNews', function(req, res, next) {
        //console.log(req.body)
        const news = req.body
        newsService.insertNews(news).then(() => {
            new Result('添加新闻成功').success(res)
        }).catch(err => {
            next(boom.badImplementation(err))
        })
    }
)

//修改新闻
router.post('/updateNews', function(req, res, next) {
        //console.log(req.body)
        const news = req.body
        newsService.updateNews(news).then(() => {
            new Result('修改新闻成功').success(res)
        }).catch(err => {
            next(boom.badImplementation(err))
        })
    }
)
//删除新闻
router.get('/deleteNews', function(req, res, next) {

    const id = req.query.id
    ////console.log(req)
    if (!id) {
        next(boom.badRequest(new Error('参数id不能为空')))
    } else {
        newsService.deleteNews(id).then(() => {
            new Result('删除新闻信息成功').success(res)
        }).catch(err => {
            next(boom.badImplementation(err))
        })
    }
})


module.exports = router
